﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UserQuanXianManager
{
	public partial class ModifyProcessStep : Form
	{
		string sProcessTyp;
 
		public ModifyProcessStep(string ProcessTyp = "BiJiaDan")
		{
			sProcessTyp = ProcessTyp;
			InitializeComponent();
		}

		private void btOK_Click(object sender, EventArgs e)
		{
			using (var context = new blackbirdEntities())
			{
				var processTypeId = context.processtype.SingleOrDefault(v => v.Name == sProcessTyp);

				var processSteps = context.process.Where(v => v.ProcessType_idProcessType == processTypeId.idProcessType);

				foreach (var process in processSteps)
				{
					context.process.DeleteObject(process);
				}
				context.SaveChanges();

				for (int i = 0; i < lbProcessStep.Items.Count; i++)
				{
					MyListBoxItem gpItem = lbProcessStep.Items[i] as MyListBoxItem;
					int groupid = int.Parse(gpItem.id);

					process ps = new process();
					ps.Group_idGroup = groupid;
					ps.ProcessType_idProcessType = processTypeId.idProcessType;
					ps.StepNumber = i;

					context.process.AddObject(ps);
				}
				context.SaveChanges();
			}

			this.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.Close();
		}

		private void ModifyProcessStep_Load(object sender, EventArgs e)
		{
			UpdateGroupSelectListBox();

			UpdateProcessStepBox();
		}

		private void UpdateProcessStepBox()
		{
			lbProcessStep.Items.Clear();

			using (var context = new blackbirdEntities())
			{
				var processTypeId = context.processtype.SingleOrDefault(v => v.Name == sProcessTyp);

				var processSteps = context.process.Where(v => v.ProcessType_idProcessType == processTypeId.idProcessType);

				var processLists = processSteps.ToList();

				processLists.Sort(ProcessComparision);

				for (int i = 0; i < processLists.Count; i++)
				{
					process ps = processLists[i];
					group gp = context.group.FirstOrDefault(v => v.idGroup == ps.Group_idGroup);

					MyListBoxItem gpItem = new MyListBoxItem();
					gpItem.id = gp.idGroup.ToString();
					gpItem.Context = gp.Name + " "+ gp.Description;
					lbProcessStep.Items.Add(gpItem);
				}

			}
		}

		private static int ProcessComparision(process x, process y)
		{
			if (x.StepNumber > y.StepNumber)
				return 1;
			if (x.StepNumber < y.StepNumber)
				return -1;

			return 0;
		}

		private void UpdateGroupSelectListBox()
		{
			cbGroupList.Items.Clear();
			using (var context = new blackbirdEntities())
			{
				var allGroup = context.group;
				foreach (var gp in allGroup)
				{
					MyListBoxItem gpItem = new MyListBoxItem();
					gpItem.id = gp.idGroup.ToString();
					gpItem.Context = gp.Name + " " + gp.Description;
					cbGroupList.Items.Add(gpItem);
				}
			}

		}

		private void tbAddGroup_Click(object sender, EventArgs e)
		{
			MyListBoxItem mBoxItem = cbGroupList.SelectedItem as MyListBoxItem;
			if (mBoxItem == null)
			{
				return;
			}

			lbProcessStep.Items.Add(mBoxItem);
		}

		private void tbDeleteProcessStep_Click(object sender, EventArgs e)
		{
			var boxItem = lbProcessStep.SelectedItem;

			if (boxItem != null)
			{
				lbProcessStep.Items.Remove(boxItem);
			}
		}
	}
}
